--Handle power ups

local sound1 = love.sound.newSoundData('assets/sounds/PowerUps/bonus_1.ogg')

local priority = 7

EntitySystem.updatesystem("powerUpCollisions", priority, function(dt)
  for e in pairs(EntitySystem.query("hasPowerUp")) do
    --Filter out powerups far away from the player
      local dist = math.dist(e.position.x,e.position.y, player.position.x,player.position.y)

      if dist <= 1000 then
        --Check for collision with player
        if e.powerUp.colType == "circle" then
          if dist <= e.powerUp.width then
            --Apply power up
            e.powerUp.action(e)
          end
        elseif e.powerUp.colType == "rect" then
          --Check for a rectangular collision
          if player.position.x >= (e.position.x - e.powerUp.width) and player.position.x <= (e.position.x + e.powerUp.width) then
            if player.position.y >= (e.position.y - e.powerUp.height) and player.position.y <= (e.position.y + e.powerUp.height) then
              --Apply power up
              e.powerUp.action(e)
            end
          end
        end
      end
  end
end)

function healthUpFunction(e)
  player.health.value = player.health.value + 20
  EntitySystem.attach(e, "audio")
  e.audio.sound = sound1
  e.audio.volume = 0.4
  e.onDeath.needsRun = true
  gameScore = gameScore + 2
end